//
//  ViewController.swift
//  UIDemo
//
//  Created by MAC on 2016/12/23.
//  Copyright © 2016年 MAC. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    /************** XIB拖拽声明指针 *****************/
    //这是拖拽的
    @IBOutlet weak var las: UILabel!
    /************** XIB拖拽声明指针 *****************/
    /************** 声明全局的 *****************/
    //声明全局的
    var btn : UIButton?
    /*
     lazy是懒性加载和懒加载是一个意思，使用这个后可以在方法里直接写坐标宽高，不需要在写label = UILabel()
     */
    lazy var labels = UILabel()
    lazy var bgView = UIView()
    lazy var yuanBtn = UIButton()
    lazy var textfields = UITextField()
    lazy var biaoTiLabel = UILabel()
    lazy var tiShiLabel = UILabel()
    var aVC = AViewController()
    var forVC = ForViewController()
    
        /************** 声明全局的 *****************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
       self.view.isMultipleTouchEnabled = true
        
        /*************************
         swift 2和swift 3中CGRectMake写法是不一样的
         swift 2 可以这么写CGRectMake(x: ;y: ;width: ;height: ) 
         
         但是，swift 3 中这个已经废除了，你只能这样写如下，你只能这样写才不会报警告
         
         let sizeBtn = CGSize(width:self.view.frame.size.width,height:100)
         let pointBtn = CGPoint(x:100,y:100)
         
         labels.frame = CGRect(origin: pointBtn,size: sizeBtn)
         *************************/
        
        let sizeBtn = CGSize(width:self.view.frame.size.width/2,height:100)
        let pointBtn = CGPoint(x:130,y:160)
        
        /************************
         这样写是不行的，会包警告的
        labels.frame = self.view.frame
         ----------------
         这样写是可以的
         let label = UIView(frame:self.view.frame);
        ************************/
        
        labels.frame = CGRect(origin: pointBtn,size: sizeBtn)
        //label设置背景颜色
        labels.backgroundColor = UIColor.cyan;
        labels.text = "我是Label"
        labels.textColor = UIColor.black
        self.view.addSubview(labels);
        
        
        initview()
        initUIButton()
        initTextField()
        initLabel()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    /************* 触摸事件（点击，移动，抬起等）说明及用例 ***************/
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        /***********************
         通知调用者当有一个或者多个手指触摸到了视图或者窗口时触发此方法。
         touches是UITouch的集合，通过UITouch我们可以检测触摸事件的属性，是单拍还是双拍，还有触摸的位置等。
         ***********************/
        print("===== Began ====")
        
        //设置键盘退出
        textfields.resignFirstResponder();
        
        
        for touch: AnyObject in touches {
            let t:UITouch = touch as! UITouch
            //当在屏幕上连续拍动两下时，背景恢复为白色
            if(t.tapCount == 2)
            {
                self.view.backgroundColor = UIColor.white
                biaoTiLabel.textColor = UIColor.orange
            }
                //当在屏幕上单击时，屏幕变为红色
            else if(t.tapCount == 1)
            {
                self.view.backgroundColor = UIColor.red
                biaoTiLabel.textColor = UIColor.black
            }
            print("event begin!")
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        /***********************
         告诉接收者一个或者多个手指在视图或者窗口上触发移动事件。
         默认不允许多点触摸。如果要接收多点触摸事件必须将UIView的属性设置为true。
         ***********************/
        print("===== Moved ====")
        
        for touch: AnyObject in touches {
            let t:UITouch = touch as! UITouch
            print(t.location(in: self.view))
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        /***********************
         当一个触摸事件结束时发出的UITouch实例对象。
         ***********************/
        print("===== Ended ====")
        
        //两点触摸时，计算两点间的距离，以及角度
        if touches.count == 2{
            //获取触摸点
            let first = (touches as NSSet).allObjects[0] as! UITouch
            let second = (touches as NSSet).allObjects[1] as! UITouch
            //获取触摸点坐标
            let firstPoint = first.location(in: self.view)
            let secondPoint = second.location(in: self.view)
            //计算两点间的距离
            let deltaX = secondPoint.x - firstPoint.x
            let deltaY = secondPoint.y - firstPoint.y
            let initialDistance = sqrt(deltaX*deltaX + deltaY*deltaY)
            print("两点间距离：\(initialDistance)")
            //计算两点间的角度
            let height = secondPoint.y - firstPoint.y
            let width = firstPoint.x - secondPoint.x
            let rads = atan(height/width);
            let degrees = 180.0 * Double(rads) / M_PI
            print("两点间角度：\(degrees)")
        }
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        /***********************
         通知接收者当系统发出取消事件的时候（如低内存消耗的告警框）
         ***********************/
        print("===== Cancelled ====")
    }
    /************* 触摸事件（点击，移动，抬起等）说明及用例 end ***************/
    
    
    func initview() {
        
        print("----- 1 -----");
        
        
        let sizeBtn = CGSize(width:self.view.frame.size.width/2,height:50)
        let pointBtn = CGPoint(x:130,y:270)
        
        bgView.frame = CGRect(origin: pointBtn,size: sizeBtn);
        bgView.backgroundColor = UIColor.orange;
        self.view.addSubview(bgView);
        
        let lab = UILabel(frame:CGRect(origin:CGPoint(x:20,y:10),size:CGSize(width:sizeBtn.width-40,height:30)));
        lab.text = "我是 View"
        lab.textColor = UIColor.white
        
        bgView.addSubview(lab);
        
        
    }
    
    func initUIButton() {
        print("----- 2 -----");
        /******************
         button的type的用法，在网上看到了不同的
         第一种是这样写的：UIButton(type: UIButtonType.custom)
         第二种是这样写的：UIButton(type: .custom)
         至于有没有什么区别，暂时不清楚，这两种写法都可以用，不会报警告的
         ******************/
        /**********************
         button的 Selector 的变化，从1.0到3.0经历了多次的变化；
         从最早的写法 @Selector("xxxClick:") 到现在3.0的写法 #selector(xxxClick(sender:)) ，就像人一样不断的进化，从出生到成年一样
         **********************/
        
        btn = UIButton(type:.custom)
        btn?.backgroundColor = UIColor.darkGray;
        btn?.tag = 100;
        btn?.frame = CGRect(origin:CGPoint(x:20,y:170),size:CGSize(width:100,height:40));
        btn?.addTarget(self, action: #selector(btnclick(sender:)), for: UIControl.Event.touchUpInside);
        btn?.setTitle("不带圆角", for: UIControl.State.normal)
        self.view.addSubview(btn!);
        
        let btn_001 = UIButton(type:.custom);
        btn_001.frame = CGRect(origin:CGPoint(x:20,y:220),size:CGSize(width:100,height:40));
        btn_001.backgroundColor = UIColor.gray;
        btn_001.tag = 101;
        btn_001.layer.cornerRadius = 5
        btn_001.setTitle("带圆角", for: UIControl.State.normal)
        btn_001.titleLabel?.font = UIFont.systemFont(ofSize: 15)//设置文字大小
        btn_001.addTarget(self, action:#selector(btnclick(sender:)), for: .touchUpInside);
        self.view.addSubview(btn_001);
        
        //设置button的type类型，custom自定义
        yuanBtn = UIButton(type:.custom)
        
        //设置坐标宽高
        yuanBtn.frame = CGRect(origin:CGPoint(x:20,y:270),size:CGSize(width:80,height:80));
        
        //设置背景色，使用color的rgb配置颜色
        yuanBtn.backgroundColor = UIColor(red:55/255,green:186/255,blue:89/255,alpha:1);
        
        //设置tag值（唯一标识符）
        yuanBtn.tag = 102
        
        //设置子视图图层超过父视图就被截取掉
        yuanBtn.layer.masksToBounds = true
        
        //这是设置边框线宽度
        yuanBtn.layer.borderWidth = 4
        
        //设置圆角，设置控件宽高的一半，则形成圆形
        yuanBtn.layer.cornerRadius = 40
        
        //添加触发事件
        yuanBtn.addTarget(self, action: #selector(btnclick(sender:)), for: UIControl.Event.touchUpInside)
        
        //设置文字
        yuanBtn.setTitle("按 钮", for: UIControl.State.normal)
        
        //设置文字颜色
        yuanBtn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        
        //设置文字大小
        yuanBtn.titleLabel?.font = UIFont.systemFont(ofSize: 25)
        
        //添加视图
        self.view.addSubview(yuanBtn);
        
        //跳转表界面
        let tiaozhuanBtn = UIButton(type:.custom)
        tiaozhuanBtn.backgroundColor = UIColor.brown
        tiaozhuanBtn.frame = CGRect(origin:CGPoint(x:20,y:490),size:CGSize(width:100,height:30))
        tiaozhuanBtn.setTitle("表界面", for: UIControl.State.normal)
        tiaozhuanBtn.tag = 103
        tiaozhuanBtn.addTarget(self, action: #selector(btnclick(sender:)), for: UIControl.Event.touchUpInside)
        self.view.addSubview(tiaozhuanBtn);
        
        let tiaozhuanBtn_001 = UIButton(type:.custom)
        tiaozhuanBtn_001.backgroundColor = UIColor.brown
        tiaozhuanBtn_001.frame = CGRect(origin:CGPoint(x:130,y:490),size:CGSize(width:100,height:30))
        tiaozhuanBtn_001.setTitle("For界面", for: UIControl.State.normal)
        tiaozhuanBtn_001.tag = 104
        tiaozhuanBtn_001.addTarget(self, action: #selector(btnclick(sender:)), for: UIControl.Event.touchUpInside)
        self.view.addSubview(tiaozhuanBtn_001);
        
        
    }
    //实现初始化输入框方法
    func initTextField()  {
        //设置坐标宽高
        textfields.frame = CGRect(origin:CGPoint(x:20,y:360) ,size:CGSize(width:200,height:30))
        //设置背景颜色
        textfields.backgroundColor = UIColor.gray
        //设置输入框样式，无边框
        textfields.borderStyle = UITextField.BorderStyle.none
        //设置占位符字体
        textfields.placeholder = "请输入"
        //设置输入框模式
        textfields.leftViewMode = UITextField.ViewMode.always
        //添加到要显示的视图上
        self.view.addSubview(textfields);
        //创建view；由于输入框的文字太靠边，所以添加一个view来作为隔离；如果有小图标可以添加到这个view上
        let leftView = UIView();
        leftView.frame = CGRect(origin:CGPoint(x:0,y:0) ,size:CGSize(width:5,height:30))
        leftView.backgroundColor = UIColor.clear
        textfields.leftView = leftView
        
    }
    
    //实现初始化方法
    func initLabel()  {
        //设置坐标宽高
        biaoTiLabel.frame = CGRect(origin:CGPoint(x:20,y:400) ,size:CGSize(width: self.view.frame.size.width-40, height:80))
                biaoTiLabel.backgroundColor = UIColor.gray
        //设置字体居中
        biaoTiLabel.textAlignment = NSTextAlignment.center
        //设置行数；0为不固定行数，1~n为固定行数，最大固定行数未知（没试过）
        biaoTiLabel.numberOfLines = 0
        //设置字体大小
        biaoTiLabel.font = UIFont.systemFont(ofSize: 15)
        //添加内容
        biaoTiLabel.text = "当在屏幕空白区域上连续拍动两下时，背景恢复为白色 \n 当在屏幕空白区域上单击时，屏幕变为红色"
        
        biaoTiLabel.textColor = UIColor.orange
        
        //添加到要显示的视图上
        self.view.addSubview(biaoTiLabel)
        
        tiShiLabel.frame = CGRect(origin:CGPoint(x:20,y:390),size:CGSize(width:self.view.frame.size.width-40,height:30))
        
        
    }
    
    //实现方法
    @objc func btnclick(sender:UIButton) {
        
        //设置一个开关，根据tag值来区分，并执行其内代码
        switch sender.tag
        {
        
            case 100:
                
                print("----- 100")//打印输出
            
            case 101:
                
                print("----- 101")//打印输出
            
            case 102:
            
                print("----- 102")//打印输出
            case 103:
            
                print("----- 103")//打印输出
                self.navigationController?.pushViewController(aVC, animated: true)
            case 104:
            
                print("----- 104")//打印输出
                //给for界面传值
                forVC.st = "快点传值过来啊"
                
                //跳转界面
                self.navigationController?.pushViewController(forVC, animated: true)
            
            break //设置结束
            
            default: break
            
            
        
        }
    }
    //这是textField代理方法，使用这个方法必须实现textField的代理方法
//    func textFieldShouldReturn(textField: UITextField!) -> Bool
//    {
//        textField.resignFirstResponder()
//        return true;
//    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

